package com.demo.springboot;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class MyConfig {

    @Bean("MyBean")
    @Scope("prototype") // 关闭单例模式
    public MyBean createMyBean() {
        return new MyBean();
    }

    @Bean()
    public RunableFactoryBean createFactoryBean() {
        return new RunableFactoryBean();
    }

    // 1.实现FactoryBean的接口
    @Bean
    public CarFactory createCarFactory() {
        return new CarFactory();
    }

    // 2.自动找到参数所对应的Bean 并装配
    @Bean
    public Car createCar(CarFactory factory) {
        return factory.creatCar();
    }

    // 1.实现接口，重写初始化和销毁
    @Bean
    public Cat createCat() {
        return new Cat();
    }

    // 3.通过注解方式初始化和销毁
    @Bean
    public Animal createAnimal() {
        return new Animal();
    }

    @Bean
    // @Primary
    public UserDao createUserDao() {
        return new UserDao();
    }

    @Bean
    public UserDao createUserDao2() {
        return new UserDao();
    }
}
